﻿using System.Linq;
using System.Xml.Linq;

namespace Extensions
{
    static class XElementExtensions
    {
        public static string AttributeValue(this XElement xElement, string attributeName)
        {
            return xElement.Attribute(attributeName).Value;
        }

        public static bool TryGetAttributeValue(this XElement xElement, string attributeName, out string value)
        {
            var att = xElement.Attribute(attributeName);
            value = (string)att;

            if (string.IsNullOrWhiteSpace(value))
                return false;
            else
                return true;

        }

        public static string AttributeValueorDefault(this XElement xElement, string attributeName, string defaultValue)
        {
            if (xElement.Attributes().Any(a => a.Name == attributeName))
            {
                return xElement.AttributeValue(attributeName);
            }

            return defaultValue;
        }
    }


}
